/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.instrument.renderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

public class ThermometerRenderer extends JComponent implements TableCellRenderer {

    protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    protected static Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");

    private Integer value;

    public ThermometerRenderer() {
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Dimension size = this.getSize();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, size.width, size.height);

        if (value != null) {
            g.setColor(Color.GREEN);
            double r = (size.height - 4) / 2.0;
            int centerX = (int) (size.width * (value.intValue()) / 20.0);
            int centerY = (int) r;
            ( (Graphics2D) g).fill(new Ellipse2D.Double(centerX - r, centerY - r + 2, r * 2, r * 2));
        }
    }

    public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (hasFocus) {
            this.setBorder(focusBorder);
        } else {
            this.setBorder(noFocusBorder);
        }
        this.value = (Integer) value;
        this.setToolTipText(value + "");
        return this;
    }
}